package com.xiaoyun.lzg.strategy;

import cn.hutool.core.map.MapUtil;
import com.xiaoyun.lzg.controller.FreeMarkerUtil;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public abstract class SuffixStrategy {

    @Autowired
    private FreeMarkerUtil freeMarkerUtil;

    public abstract Object marker(String name, InputStream is);

    public abstract String getType();


    public FreeMarkerUtil getFreeMarkerUtil() {
        return freeMarkerUtil;
    }

    public List<Map<String, Object>> mapSave(File outFile) {
        Map<String, String> map = new HashMap<>();
        this.freeMarkerUtil.listTxT(outFile, map);
        List<Map<String, Object>> list = new ArrayList<>();
        map.forEach((key, value) -> {
            Map<String, Object> one = saveOne(value, key);
            if (one != null && one.size() > 0) {
                list.add(one);
            }
        });
        return list;
    }

    public Map<String, Object> saveOne(String tempUrl, String fileName) {

        Map<String, Object> root = freeMarkerUtil.getEntity(tempUrl, fileName);
        if (MapUtil.isEmpty(root)) {
            return null;
        }
        freeMarkerUtil.initMap(fileName, root);
        //TODO 生成代码Bean
        try {
            freeMarkerUtil.generate(root);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return root;
    }
}
